/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using System.Drawing;
using FlickrNet;

namespace ArtNotes
{
	/// <summary>
	/// This is used to initially allow access to a users
	/// flickr account by the app.
	/// </summary>
	public class ANFlickrSetup : UIViewController {
		UIWebView web;

		public ANFlickrSetup(IntPtr p) : base(p)
		{
		}
		
		public ANFlickrSetup()
		{
		}
	
		public override void ViewWillDisappear (bool animated)
		{
			web.StopLoading ();
			web.Delegate = null;
			UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
		}
		
		public override void ViewDidLoad ()
		{
			Title = "Flickr Setup";
			var webFrame = UIScreen.MainScreen.ApplicationFrame;
			//webFrame.Y += 25f;
			//webFrame.Height -= 40f;
	
			web = new UIWebView (new RectangleF(0,-25,320,480)) {
				BackgroundColor = UIColor.White,
				ScalesPageToFit = true,
				AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
			};
			web.LoadStarted += delegate {
				UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
			};
			web.LoadFinished += delegate {
				UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
			};
			web.LoadError += (webview, args) => {
				UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
				web.LoadHtmlString (String.Format ("<html><center><font size=+5 color='red'>An error occurred:<br>{0}</font></center></html>", args.Error.LocalizedDescription), null);
			};
			View.AddSubview (web);
	
			// Delegate = new 
			var urlField = new UITextField (new RectangleF (20f, 10f, View.Bounds.Width - (20f * 2f), 30f)){
				BorderStyle = UITextBorderStyle.Bezel,
				TextColor = UIColor.Black,
				Placeholder = "<enter a URL>",
				Text = "http://www.mono-project.com/",
				BackgroundColor = UIColor.White,
				AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
				ReturnKeyType = UIReturnKeyType.Go,
				KeyboardType = UIKeyboardType.Url,
				AutocapitalizationType = UITextAutocapitalizationType.None,
				AutocorrectionType = UITextAutocorrectionType.No,
				ClearButtonMode = UITextFieldViewMode.Always
			};
	
			urlField.ShouldReturn = delegate (UITextField field){
				field.ResignFirstResponder ();
				web.LoadRequest (NSUrlRequest.FromUrl (new NSUrl (field.Text)));
	
				return true;
			};
	
			//View.AddSubview (urlField);
			
			string apikey = "5b5e453ad39ab5770f49ca73debeda40";
			string secret = "097f8bb3121875a8";
			string frob = "";
			string authUrl = "";
			
			Flickr flickr = new Flickr();
			flickr.ApiKey = apikey;
			flickr.ApiSecret = secret;
			
			frob = flickr.AuthGetFrob();
			
			authUrl = flickr.AuthCalcUrl(frob,AuthLevel.Write);
			
			Console.WriteLine("frob is: " + frob);
			Console.WriteLine("Calculated URL is: " + authUrl);
			
			//PhotoSearchOptions searchOptions = new PhotoSearchOptions();
			//searchOptions.Tags = "landscape";
			
			//Photos flickrPhotos = flickr.PhotosSearch(searchOptions);
			
			//Console.WriteLine("Total pictures: " + flickrPhotos.TotalPhotos.ToString());
			
			//searchOptions.Page = 2;
			//Photos flickrPhotos2 = flickr.PhotosSearch(searchOptions);
			
			//PhotoCollection foundPhotos = flickrPhotos.PhotoCollection;
			
			//allPhotos.AddRange(flickrPhotos2.PhotoCollection);
			
			//Console.WriteLine("Total Photos: " + flickrPhotos.PhotoCollection.Length.ToString());
			

			//foreach (Photo p in foundPhotos)
			//{
			//	Console.WriteLine("Photo title is " + p.Title);
			//	Console.WriteLine("Photo URL is " + p.MediumUrl);
				//downloadImage(p.MediumUrl,p.Title + "_" + p.PhotoId + ".jpg");
			//}
			
			web.ScalesPageToFit = true;
			web.LoadRequest (NSUrlRequest.FromUrl (new NSUrl (authUrl)));
		}
	}
}
